﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RobozzleModel;
using System.Web.Security;


public enum MenuPage
{
    Scoreboard,
    Puzzles,
    Forums,
    None
}

/// <summary>
/// Summary description for StatsCommon
/// </summary>
public static class StatsCommon
{
    public const int BAD_PUZZLE_THRESHOLD = -5; // Puzzles with liked at or below this threshold will be invisible

    public const int DIFFICULTY_VOTE = 0;

    public static string Footer()
    {
        return "<p style=\"font-size: small; margin-top: 20px;\">©2009 <a href=\"http://igoro.com/\">Igor Ostrovsky</a>. <a href=\"/tou.aspx\">Terms of use</a></p>";
    }

    public static string MenuBar(MenuPage activePage)
    {
        Func<bool, string, string, string> format =
            (active, url, label)
                => string.Format(
                    "{0}{1}{2}",
                    active ? "<b>" : ("<a href=\"" + url + "\">"),
                    label,
                    active ? "</b>" : "</a>");

        return
            "<p class=\"menubar\">"
            + format(activePage == MenuPage.Scoreboard, "/scoreboard.aspx", "Scoreboard")
            + " | "
            + format(activePage == MenuPage.Puzzles, "/puzzles.aspx", "Puzzles")
            + " | "
            + format(activePage == MenuPage.Forums, "/forums/", "Forums")
            + "</p>"
            + "<div style=\"position: absolute; top: 20px; right: 80px\"><a href=\"/myprofile.aspx\">Edit my profile</a></div>";
    }

    public static string MakeForumPath(int categoryId, string category, string thread)
    {
        string path = "<a href=\"/forums/\">Forums</a> / ";
        if (thread != null)
        {
            path += "<a href=\"/forums/default.aspx?category=" + categoryId + "\">" + category + "</a> / " + thread;
        }
        else
        {
            path += category;
        }
        return path;
    }

    public static void ConvertVotes(LevelWithDifficulty level, out string likedness, out string difficulty)
    {
        int liked = (level.CoolnessVoteSum + level.CoolnessVoteCount) / 2;
        int disliked = level.CoolnessVoteCount - liked;
        likedness = string.Format("+{0} / -{1}", liked, disliked);
        difficulty = level.DifficultyVoteCount > 0 
            ? String.Format("{0:0.00}",(1.0*level.DifficultyVoteSum / level.DifficultyVoteCount))
            : "?";
    }

    public static string RowClass(int rowIndex)
    {
        return rowIndex%2 == 0 ? "evenRow" : "oddRow";
    }

    public static string UserLink(string userName)
    {
        return string.Format("<a href=\"/user.aspx?name={0}\">{0}</a>", userName);
    }

    public static string FormatDate(DateTime date)
    {
        TimeSpan tm = DateTime.Now - date;
        long minutes = (long)tm.TotalMinutes;
        if (minutes <= 0) minutes = 0;
        if (minutes < 60) return Plural(" minute", minutes) + " ago";

        long hours = (long)tm.TotalHours;
        if (hours < 24) return Plural(" hour", hours) + " ago";

        long days = (long)tm.TotalDays;
        if (days < 30) return Plural(" day", days) + " ago";

        long months = days / 30;
        return Plural(" month", months) + " ago";

    }

    public static string Plural(string root, long count)
    {
        return count + " " + ((count == 1) ? root : root + "s");
    }


    /// <summary>
    /// Creates a link to a thread
    /// 
    /// Note: title is a html-encoded version of the title, so may be different from thread.Title
    /// </summary>
    public static object ThreadLink(ForumThread thread, string title)
    {
        string link = thread.PuzzleId == null ? ("thread.aspx?id=" + thread.Id) : ("thread.aspx?puzzle=" + thread.PuzzleId);
        return string.Format("<a href=\"{0}\">{1}</a>", link, title);
    }


    private static string salt = "5A6fKpgSnXoMpxbcHcb7";
    public static string HashPassword(string pwd)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(pwd + salt, "sha1");
    }
}

public class OddEven
{
    private bool m_even = true;
    public string Next()
    {
        m_even = !m_even;
        return m_even ? "odd" : "even";
    }
}
